package com.abc.robot01.vivemodel

import android.util.Log
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abc.robot01.fragment.QAFragment.DataA
import com.abc.robot01.room.DatabaseContext
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class QAFragmentViewModel : ViewModel() {
    val TAG="QAFragmentViewmodel"

    val loading =  mutableStateOf(true)

    // 使用 StateFlow 来存储列表数据
    private val _items = MutableStateFlow<MutableList<DataA>>(mutableListOf())
    val items: StateFlow<MutableList<DataA>> = _items

    val  questionList = mutableListOf<DataA>()

    fun getData() {
        viewModelScope.launch(IO) {
            loading.value = true
            DatabaseContext.getCmdDao().queryAll().forEach {
                val questions = DatabaseContext.getChatQuestionDao().queryCustomQuestionByCmdId(it.id.toString())
                questionList.add(DataA(cmdId=it.id, question = questions[0].question, date = questions[0].date ))
                Log.e(TAG,""+questions[0]);
            }
            _items.value = questionList
            loading.value = false
        }
    }

    fun searcByQuestion(value: String) {
        questionList.clear()
//        DatabaseContext.getChatQuestionDao().queryByQuestion(value).forEach {
//            val questions = DatabaseContext.getChatQuestionDao().queryCustomQuestionByCmdId(it.cmd_id)

//        }
    }

    init {
        getData()
    }
}

